#include "ui_message.h"

void ui_message_send(UIMessageBase* msg) {
    if (msg == NULL) {
        LV_LOG_ERROR("ui_message_send: msg is NULL");
        return;
    }
LV_LOG_ERROR("ui_message_send: g_ui_message_queue is undefined");
}
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <errno.h>

void ui_message_queue_init(ThreadSafeUIMessageQueue* queue, size_t capacity) {
    queue->messages = malloc(sizeof(UIMessageBase*) * capacity);
    queue->capacity = capacity;
    queue->head = queue->tail = queue->count = 0;
    pthread_mutex_init(&queue->lock, NULL);
    pthread_cond_init(&queue->not_empty, NULL);
    pthread_cond_init(&queue->not_full, NULL);
}

void ui_message_queue_destroy(ThreadSafeUIMessageQueue* queue) {
    // 清空队列中剩余的消息
    pthread_mutex_lock(&queue->lock);
    while (queue->count > 0) {
        UIMessageBase* msg = queue->messages[queue->head];
        queue->head = (queue->head + 1) % queue->capacity;
        queue->count--;
        
        if (msg->free_fn) {
            msg->free_fn(msg);
        } else {
            free(msg);
        }
    }
    pthread_mutex_unlock(&queue->lock);
    
    // 销毁同步原语
    pthread_mutex_destroy(&queue->lock);
    pthread_cond_destroy(&queue->not_empty);
    pthread_cond_destroy(&queue->not_full);
    
    free(queue->messages);
}

bool ui_message_queue_push(ThreadSafeUIMessageQueue* queue, UIMessageBase* msg) {
    pthread_mutex_lock(&queue->lock);
    
    while (queue->count == queue->capacity) {
        // 队列满，等待非满条件
        if (pthread_cond_wait(&queue->not_full, &queue->lock) != 0) {
            pthread_mutex_unlock(&queue->lock);
            return false;
        }
    }
    
    queue->messages[queue->tail] = msg;
    queue->tail = (queue->tail + 1) % queue->capacity;
    queue->count++;
    
    pthread_cond_signal(&queue->not_empty);
    pthread_mutex_unlock(&queue->lock);
    return true;
}

UIMessageBase* ui_message_queue_pop(ThreadSafeUIMessageQueue* queue) {
    pthread_mutex_lock(&queue->lock);
    
    while (queue->count == 0) {
        // 队列空，等待非空条件
        if (pthread_cond_wait(&queue->not_empty, &queue->lock) != 0) {
            pthread_mutex_unlock(&queue->lock);
            return NULL;
        }
    }
    
    UIMessageBase* msg = queue->messages[queue->head];
    queue->head = (queue->head + 1) % queue->capacity;
    queue->count--;
    
    pthread_cond_signal(&queue->not_full);
    pthread_mutex_unlock(&queue->lock);
    return msg;
}

UIMessageBase* ui_message_queue_try_pop(ThreadSafeUIMessageQueue* queue) {
    if (!queue) return NULL;
    
    pthread_mutex_lock(&queue->lock);
    
    if (queue->count == 0) {
        pthread_mutex_unlock(&queue->lock);
        return NULL;
    }
    
    UIMessageBase* msg = queue->messages[queue->head];
    queue->head = (queue->head + 1) % queue->capacity;
    queue->count--;
    
    pthread_cond_signal(&queue->not_full);
    pthread_mutex_unlock(&queue->lock);
    return msg;
}

UILabelUpdateMsg* create_label_update_msg(lv_obj_t* target, const char* text) {
    UILabelUpdateMsg* msg = malloc(sizeof(UILabelUpdateMsg));
    msg->base.type = MSG_UPDATE_LABEL_TEXT;
    msg->base.target = target;
    msg->base.free_fn = free;
    msg->text = strdup(text);
    return msg;
}

UIStyleUpdateMsg* create_style_update_msg(lv_obj_t* target, lv_style_t* style, lv_part_t part, lv_state_t state) {
    UIStyleUpdateMsg* msg = malloc(sizeof(UIStyleUpdateMsg));
    msg->base.type = MSG_UPDATE_LABEL_STYLE;
    msg->base.target = target;
    msg->base.free_fn = free;
    msg->style = style;
    msg->part = part;
    msg->state = state;
    return msg;
}

UIAsyncCallbackMsg* create_async_callback_msg(void (*callback)(void*), void* user_data) {
    UIAsyncCallbackMsg* msg = malloc(sizeof(UIAsyncCallbackMsg));
    msg->base.type = MSG_ASYNC_CALLBACK;
    msg->base.target = NULL;
    msg->base.free_fn = free;
    msg->callback = callback;
    msg->user_data = user_data;
    return msg;
}

bool ui_message_queue_push_timeout(ThreadSafeUIMessageQueue* queue, UIMessageBase* msg, int timeout_ms) {
    /* 参数校验 */
    if (!queue || !msg || timeout_ms < 0) {
        if (msg) {
            if (msg->free_fn) msg->free_fn(msg);
            else free(msg);
        }
        return false;
    }

    /* 精确超时计算 */
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    ts.tv_nsec += (timeout_ms % 1000) * 1000000;
    ts.tv_sec += timeout_ms / 1000 + ts.tv_nsec / 1000000000;
    ts.tv_nsec %= 1000000000;

    /* 线程安全保护 */
    pthread_mutex_lock(&queue->lock);
    
    /* 队列状态检查 */
    if (!queue->messages) {
        pthread_mutex_unlock(&queue->lock);
        if (msg->free_fn) msg->free_fn(msg);
        else free(msg);
        return false;
    }

    /* 带超时的队列满等待 */
    while (queue->count == queue->capacity) {
        int rc = pthread_cond_timedwait(&queue->not_full, &queue->lock, &ts);
        if (rc == ETIMEDOUT) {
            pthread_mutex_unlock(&queue->lock);
            if (msg->free_fn) msg->free_fn(msg);
            else free(msg);
            return false;
        }
        if (rc != 0) {
            pthread_mutex_unlock(&queue->lock);
            if (msg->free_fn) msg->free_fn(msg);
            else free(msg);
            return false;
        }
    }

    /* 消息入队 */
    queue->messages[queue->tail] = msg;
    queue->tail = (queue->tail + 1) % queue->capacity;
    queue->count++;

    /* 智能条件通知 */
    if (queue->count == 1) {
        pthread_cond_signal(&queue->not_empty);
    }

    pthread_mutex_unlock(&queue->lock);
    return true;
}